//
// Copyright (c) 2009 All Right Reserved
//
// Stephen Toub
// stoub@microsoft.com
// 2009-01-01
// Contains ...
using System;
using System.Globalization;
using System.IO;
using System.Text;
using JetBrains.Annotations;
namespace LargoCommon.Midi {
/// Represents a text meta event message.
[Serializable]
public abstract class MetaAbstractText : MetaEvent {
#region Fields
/// The text associated with the event.
private string text;
#endregion
#region Constructors
/// Initializes a new instance of the MetaAbstractText class.
/// The amount of time before this event.
/// The ID of the meta event.
/// The text associated with the event.
protected MetaAbstractText(long deltaTime, byte givenMetaEventId, string text) :
base(deltaTime, givenMetaEventId) {
this.text = text;
}
#endregion
#region Properties
/// Gets or sets the text associated with this event.
/// General musical property.
public string Text {
get => this.text;
[UsedImplicitly]
set => this.text = value ?? throw new ArgumentNullException(nameof(value));
}
#endregion
#region To String
/// Generate a string representation of the event.
/// A string representation of the event.
public override string ToString() {
var sb = new StringBuilder();
sb.Append(base.ToString());
sb.Append("\t");
if (this.Text != null) {
sb.Append(this.Text.ToString(CultureInfo.CurrentCulture));
}
return sb.ToString();
}
#endregion
#region Methods
/// Write the event to the output stream.
/// The stream to which the event should be written.
public override void Write(Stream outputStream) {
if (outputStream == null) {
return;
}
//// Write out the base event information
base.Write(outputStream);
if (this.text == null)
{
return;
}
//// Special meta event marker and the id of the event
var asciiBytes = Encoding.ASCII.GetBytes(this.text);
MidiEvent.WriteVariableLength(outputStream, asciiBytes.Length);
outputStream.Write(asciiBytes, 0, asciiBytes.Length);
}
#endregion
}
}